package com.marketlive.app.b2c.account;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMessages;
import com.marketlive.app.b2c.struts.XmlValidatingDispatchAction;
import com.marketlive.app.b2c.common.constants.RequestParams;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Parent Dispatch Action class for action in My Account. For details about parameters of methods please see the package
 * description.
 */

public class AbstractAccountAction extends XmlValidatingDispatchAction {
    private static Log log = LogFactory.getLog(AbstractAccountAction.class);

	/**
	 * Executes default method (view) if method parameter is not specified in the subclass action.
	 *
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	protected ActionForward unspecified(ActionMapping mapping,
										ActionForm form,
										HttpServletRequest request,
										HttpServletResponse response) throws Exception {
		return view(mapping, form, request, response);
	}

    public ActionForward view(  ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response )
        throws Exception
    {
        if (log.isDebugEnabled()) {
            log.debug( "view() called on: " + this );
        }

        return mapping.getInputForward();
    }

    public ActionForward submit(  ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response )
        throws Exception
    {
        if (log.isDebugEnabled()) {
            log.debug( "submit() called on: " + this +" forwarding to :"+ mapping.getInputForward().getPath());
        }
        return mapping.getInputForward();
    }

    public ActionMessages validate(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request) {

        // if view, don't call validate
        try {
            String method = getMethodName(mapping, form, request, null, RequestParams.METHOD);
            if (RequestParams.METHOD_VIEW.equals(method)) {
                log.debug("skipping validate for view request");
                return null;
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

        return super.validate(mapping,form,request);
    }
}
